package cn.lyw.seckill;


import cn.common.init.InitDataConstant;
import cn.common.redis.RedisServie;
import cn.common.restresult.util.RestResult;
import cn.common.restresult.util.ResultEnum;
import cn.lyw.seckillgoods.mapper.pojo.SmallMiSeckillGoods;
import cn.lyw.seckillgoods.service.service.SeckillGoodsService;
import cn.lyw.seckillsession.service.service.SeckillSessionService;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author   lyw
 * @Description 用户秒杀
 * @Date 9:09 2021/7/26
 * @Param
 * @return
 */
@Component
public class UserSeckill {

    @Autowired
    private SeckillSessionService seckillSessionService;

    @Autowired
    private SeckillGoodsService seckillGoodsService;

    @Autowired
    private Redisson redisson;

    @Autowired
    private RedisServie redisServie;


    /**
     * @Author   lyw
     * @Description 秒杀池
     * @Date 9:26 2021/7/26
     * @Param
     * @return
     */
//    public List<SmallMiSeckillGoods> seckillPool(String sessionId){
//
//        //先将redis中本场次的商品取出
//        List<SmallMiSeckillGoods> list = redisServie.getList(InitDataConstant.SECKILL_SESSION_SECKILLACTIVITY + sessionId, SmallMiSeckillGoods.class);
//
//        //将数量为0的商品移出秒杀池
//        for (SmallMiSeckillGoods smallMiSeckillGoods : list) {
//
//             if (smallMiSeckillGoods.getSeckillCount()<=0){
//                 list.remove(smallMiSeckillGoods);
//             }
//
//        }
//
//        return list;
//    }


    /**
     * @Author   lyw
     * @Description 秒杀
     * @Date 9:43 2021/7/26
     * @Param
     * @return
     */
    public RestResult<SmallMiSeckillGoods> seckill(UserSeckillDTO dto){

        String lockKey="lockKey";

        //获取分布式锁对象
        RLock lock = redisson.getLock(lockKey);

        try {
            //上锁
            lock.lock();

            //TODO
            //如果没有登录
            if (dto.getMiId()==null){

                System.out.println("请登录");

                return new RestResult<>(ResultEnum.ERROR);
            }

            //先将redis中本场次的商品取出
            List<SmallMiSeckillGoods> list = redisServie.getList(InitDataConstant.SECKILL_GOODS + dto.getSessionId(), SmallMiSeckillGoods.class);

            //TODO
            //查询该用户订单信息中有没有本场次的商品


            //根据参数的spuID和skuId查看商品的数量是否足够
            for (SmallMiSeckillGoods smallMiSeckillGoods : list) {
                //先判断商品中有没有
                if (smallMiSeckillGoods.getSpuId().equals(dto.getSpuId()) && smallMiSeckillGoods.getSkuId().equals(dto.getSkuId())){

                    //获取库存
                    Integer seckillCount = smallMiSeckillGoods.getSeckillCount();

                    //判断库存
                    if (seckillCount>0){

                        //减库存
                        smallMiSeckillGoods.setSeckillCount(seckillCount-1);

                        //测试
                        System.out.println("库存为"+seckillCount+"----抢到了");

                        //将修改后的数据重新存入redis
                        redisServie.setList(InitDataConstant.SECKILL_GOODS + dto.getSessionId(),list);

                        //返回抢到的商品
                        return new RestResult<>(smallMiSeckillGoods);

                    }else {

                        //测试
                        System.out.println("没货了");

                        //库存为0时，返回没有抢到
                        return new RestResult<>(ResultEnum.ERROR);
                    }


                }else {

                    //测试
                    System.out.println("没有该商品");

                    //没有就返回没有该商品
                    return new RestResult<>(ResultEnum.SELECT_NULL);
                }

            }

        }finally {
            //释放锁
            lock.unlock();
        }

         return null;
    }


}
